#include <NTPClient.h>
#include <WiFiUdp.h>
#include <eeprom_.h>

// String ntp1 = "120.25.115.20";  // ntp1.aliyun.com 阿里
// String ntp2 = "106.55.184.199"; // time1.cloud.tencent.com 腾讯
// String ntp3 = "101.6.6.172";    // ntp.tuna.tsinghua.edu.cn 清华
extern void open_relay();
extern void close_relay();

WiFiUDP ntpUDP;
NTPClient timeClient = NTPClient(ntpUDP);
int ntp_minutes = 0;
int ntp_second = 0;
int mode1_flag = 3;
boolean mode2_flag = true;
boolean mode3_flag = true;

int mode1_open_hour = 0; // 每日模式打开时间
int mode1_open_minute = 0;
int mode1_close_hour = 0; // 每日模式关闭时间
int mode1_close_minute = 0;

long mode2_opening_second = 0;  // 循环模式开启时长(秒)
long mode2_closeing_second = 0; // 循环模式关闭时长(秒)
long mode3_opening_second = 0;  // 倒计时模式时长(秒)
int time_enable = 0;            // 定时开启判断
int mode = 0;                   // 模式
void time_task_init()
{
    timeClient.update();
    mode1_open_hour = eeprom_get_int(MODE1_OPEN_HOUR);
    mode1_open_minute = eeprom_get_int(MODE1_OPEN_MINUTE);
    mode1_close_hour = eeprom_get_int(MODE1_CLOSE_HOUR);
    mode1_close_minute = eeprom_get_int(MODE1_CLOSE_MINUTE);
    mode2_opening_second = eeprom_get_mode2_opening_second();
    mode2_closeing_second = eeprom_get_mode2_closeing_second();
    mode3_opening_second = eeprom_get_mode3_opening_second();
    ntp_minutes = timeClient.getMinutes();
    ntp_second = timeClient.getSeconds();
    mode1_flag = 3;
    mode2_flag = true;
    mode3_flag = true;
    time_enable = eeprom_get_int(TIME_ENABLE_INDEX);
    mode = eeprom_get_int(MODE_CHOOSE_INDEX);
    if(time_enable==0&&(mode==1||mode==2)){
        open_relay();
    }
}
void ntp_client_setup()
{
    String ntp_server = eeprom_get_string(NTP_INDEX);
    timeClient.setPoolServerName(ntp_server.c_str());
    // 上海时区(+08:00)
    timeClient.setTimeOffset(60 * 60 * 8);
    // 8小时更新一次时间,离线则8:00开始
    timeClient.setUpdateInterval(60 * 60 * 1000 * 8);
    timeClient.begin();

    time_task_init();
}

void ntp_client_loop()
{
    // 每秒检查一次
    if (ntp_second != timeClient.getSeconds())
    {
        timeClient.update();
        // 实际经过秒数
        int tmp = timeClient.getSeconds() - ntp_second;
        if (tmp < 0)
        {
            tmp += 60;
        }
        // mode==1
        if (time_enable == 0 && mode == 0)
        {
            if (eeprom_get_int(MODE1_WEEK_0_ENABLE+timeClient.getDay())==0&& timeClient.getHours() == mode1_open_hour && timeClient.getMinutes() == mode1_open_minute && mode1_flag != 1)
            {
                mode1_flag = 1;
                open_relay();
            }
            if (eeprom_get_int(MODE1_WEEK_0_ENABLE+timeClient.getDay())==0 && timeClient.getHours() == mode1_close_hour && timeClient.getMinutes() == mode1_close_minute && mode1_flag != 2)
            {
                mode1_flag = 2;
                close_relay();
            }
        }
        // mode==2
        else if (time_enable == 0 && mode == 1)
        {
            if (mode2_flag)
            {
                mode2_opening_second -= tmp;
                if (mode2_opening_second <= 0)
                {
                    close_relay();
                    mode2_flag = !mode2_flag;
                    mode2_opening_second = 0;
                    mode2_closeing_second = eeprom_get_mode2_closeing_second();
                }
            }
            else
            {
                mode2_closeing_second -= tmp;
                if (mode2_closeing_second <= 0)
                {
                    open_relay();
                    mode2_flag = !mode2_flag;
                    mode2_closeing_second = 0;
                    mode2_opening_second = eeprom_get_mode2_opening_second();
                }
            }
        }
        // mode==3
        else if (time_enable == 0 && mode == 2)
        {
            if (mode3_flag)
            {
                mode3_opening_second -= tmp;
                if (mode3_opening_second <= 0)
                {
                    close_relay();
                    mode3_flag = false;
                }
            }
        }
        // Serial.printf("mode1 ");
        // Serial.printf(String(mode1_open_hour).c_str());
        // Serial.printf(":");
        // Serial.printf(String(mode1_open_minute).c_str());
        // Serial.printf(" ");
        // Serial.printf(String(mode1_close_hour).c_str());
        // Serial.printf(":");
        // Serial.printf(String(mode1_close_minute).c_str());
        // Serial.printf("      ");
        // Serial.printf("mode2 ");
        // Serial.printf(String(mode2_opening_second).c_str());
        // Serial.printf(" ");
        // Serial.printf(String(mode2_closeing_second).c_str());
        // Serial.printf("      ");
        // Serial.printf("mode3 ");
        // Serial.printf(String(mode3_opening_second).c_str());
        // Serial.printf("      ");

        // Serial.printf("ntp ");
        // Serial.printf(String(timeClient.getDay()).c_str());
        // Serial.printf(" ");
        // Serial.printf(String(timeClient.getHours()).c_str());
        // Serial.printf(" ");
        // Serial.printf(String(timeClient.getMinutes()).c_str());
        // Serial.printf(" ");
        // Serial.printf(String(timeClient.getSeconds()).c_str());
        // Serial.printf(" ");
        // Serial.printf("\n");
        
        ntp_minutes = timeClient.getMinutes();
        ntp_second = timeClient.getSeconds();
    }
}
